package com.woniuxy.store.service.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.UUID;

@Component
public class QrCodeUtils {


    @Autowired
    private OSSUtil ossUtil;
    /**
     * 生成普通类型的二维码
     */
    public String  generateGeneralQrCode(Integer storeId) throws IOException {
        // 三个参数分别是 扫码内容 宽高
        BufferedImage image = QrCodeUtil.generate("http://www.baidu.com/"+storeId,
                500, 500);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, "png", os);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(os.toByteArray());
        String originalFilename = "aa.png";
        String suffix = FileUtil.getSuffix(originalFilename);
        String fileName = UUID.randomUUID().toString().replaceAll("-", "");
        return ossUtil.uploadFile(inputStream, fileName + "." + suffix, "zjr-bucket");

//        return "data:image/png;base64," + Base64.encode(outputStream.toByteArray());
    }

}
